package com.hq.fxh.No6UerSpace.Fragment;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.widget.FrameLayout;

import com.google.gson.Gson;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.CityModle;
import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No5_User.CitySelectActivity;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/26
 * @Email xlwenhao1858@sina.com
 */


public class Select extends BaseActivity {

    protected FrameLayout fragmentCitySelect;
    private Fragment_city2Select fragment_city2Select;
    private Fragment_citySelect fragment_pro;
    private List<CityModle.DataBean> prodata;
    private FragmentManager supportFragmentManager;
    private CityModle.DataBean.CitysBean cityData;
    private String cityName;
    private int proID;
    private String proName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_city_select;
    }

    @Override
    protected void initView() {
        fragmentCitySelect = (FrameLayout) findViewById(R.id.fragment_citySelect);

    }

    @Override
    protected void initData() {
        fragment_pro = new Fragment_citySelect();


        fragment_pro.setCityCallBack(new Fragment_citySelect.CityCallBack() {
            @Override
            public void selectCall(int position, Object data) {
                LogUtils.e("  监考老师大家反馈萨拉弗利萨");
                CityModle.DataBean dataBean = prodata.get(position);
                proID = dataBean.getArea_id();
                proName = dataBean.getArea_name();
                callCity(position);
            }

            private void callCity(int position) {
                CityModle.DataBean dataBean = prodata.get(position);
                List<CityModle.DataBean.CitysBean> citys = dataBean.getCitys();
                fragment_city2Select = new Fragment_city2Select(citys);
                fragment_city2Select.setCityCallBack(new Fragment_city2Select.CityCallBack() {
                    public int cityID;

                    @Override
                    public void selectCall(int position, Object data) {
                        cityData = (CityModle.DataBean.CitysBean) data;
                        cityID = cityData.getArea_id();
                        cityName = cityData.getArea_name();
                        Intent data1 = new Intent();
                        data1.putExtra("cityName", cityName);
                        data1.putExtra("proName", proName);
                        data1.putExtra("cityID", cityID);
                        data1.putExtra("proID", proID);
                        Select.this.setResult(11, data1);
                        finish();
                    }
                });

                supportFragmentManager = getSupportFragmentManager();
                supportFragmentManager.beginTransaction().replace(R.id.fragment_citySelect, fragment_city2Select).commit();
            }
        });

        supportFragmentManager = getSupportFragmentManager();
        supportFragmentManager.beginTransaction().replace(R.id.fragment_citySelect, fragment_pro).commit();

        getCityData();
    }


    public void getCityData() {
        UserServiceImpl.getCity(new Callback<ResponseBody>() {

            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                ResponseBody body = response.body();
                if (body != null) {
                    try {
                        String string = body.string();
                        CityModle cityModle = new Gson().fromJson(string, CityModle.class);
                        prodata = cityModle.getData();
                        fragment_pro.setProData(prodata, 0);
                        LogUtils.e(string);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    ResponseBody responseBody = response.errorBody();
                    if (response.body() != null) {
                        try {
                            LogUtils.e(responseBody.string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                LogUtils.e("error  " + t.getMessage());
            }
        });
    }
}
